package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.search_service;
import entity.*;

public class search_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private search_service sc_service=new search_service();
	public search_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=(user)session.getAttribute("user");
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/search_msg")){
			String name=request.getParameter("name");
			String type=request.getParameter("type");
			boolean is_edit=false;
			if(u!=null&&u.getType()==2){
				is_edit=true;
			}
			request.setAttribute("is_edit",is_edit);
			if (type.equals("英雄")){
				List<hero>res=sc_service.search_hero(name);
				if (res.isEmpty()){
					request.getRequestDispatcher("/search/search_failure.jsp").forward(request,response);
					return;
				}
				request.setAttribute("heros",res);
				request.getRequestDispatcher("/guide/hero_guide.jsp").forward(request,response);
				return;
			}
			else if (type.equals("装备")){
				List<equip>res=sc_service.search_equip(name);
				if (res.isEmpty()){
					request.getRequestDispatcher("/search/search_failure.jsp").forward(request,response);
					return;
				}
				request.setAttribute("equips",res);
				request.getRequestDispatcher("/guide/equip_guide.jsp").forward(request,response);
				return;
			}
			else if (type.equals("海克斯")){
				List<hex>res=sc_service.search_hex(name);
				if (res.isEmpty()){
					request.getRequestDispatcher("/search/search_failure.jsp").forward(request,response);
					return;
				}
				request.setAttribute("hexs",res);
				request.getRequestDispatcher("/guide/hex_guide.jsp").forward(request,response);
				return;
			}
			else{
				List<trammel>res=sc_service.search_trammel(name);
				if (res.isEmpty()){
					request.getRequestDispatcher("/search/search_failure.jsp").forward(request,response);
					return;
				}
				request.setAttribute("trammels",res);
				request.getRequestDispatcher("/guide/trammel_guide.jsp").forward(request,response);
				return;
			}
		}

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
